<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/3
 * Time: 17:15
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;

use Illuminate\Support\Facades\Request;
use DB;
use \Illuminate\Support\Facades\Input;
use App\ModelStatistic\StatisticCommon;

class PlayerForm extends Model{

    public static function paginate()
    {
        $perPage = Request::get('per_page', 20);
        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        $tableAccount = 'platsdk_order';
        $rw = \DB::connection('mysql_bgac')->table($tableAccount)->select('cp_account','cp_player_id','cp_curr_server','cp_area',DB::raw('sum(cp_rmb) as sum'), DB::raw('count(id) as total'),'cp_rmb')->groupby('cp_player_id','cp_area');
        self::SourceCondition($rw);
        $total = $rw->get()->count();
        $rw = $rw->skip($start)->take($perPage);
        $data = $rw->get();
        $data = $data->toArray();
//        var_dump($data);exit();

        foreach ($data as &$obj) {

            // 换算分 到 元  涵盖 美元和RMB
            $obj->sum = $obj->sum / 100;
        }
        \App\Models\AccountInfo::attachAccountCreateTime($data, 'cp_account', 'created_acc_time');
        
        // $data = json_decode($data, true);
        \Log::debug(__METHOD__, [$rw->toSql()]);
        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());
//var_dump($paginator);exit();
        return $paginator;
    }


    //模糊查询
    public static function SourceCondition(&$rw)
    {
        $cond = Request::all();
        if ($cond['cp_player_id']){
            $rw->where("cp_player_id", 'like', $cond["cp_player_id"].'%');
        }
//        if ($cond["cp_player_id"]){
//            $rw->where("cp_player_id", $cond['cp_player_id'] ?: $cond['cp_player_id']);
//        }

        $created_time = Request::get('created_time', []);
                
        $start_time = StatisticCommon::GetIntTime($created_time['start'] ? : $area_extend['open_time']);
        if ($start_time) {
            $rw->where('created_time', ">=",  $start_time);
        }

        $end_time = StatisticCommon::GetIntTime($created_time['end']);
        if ($end_time) {
            $rw->where('created_time', "<=", $end_time );
        }

        $cond["charge_ok"] = Input::get('charge_ok', 1);
        if ($cond["charge_ok"]){
            $rw->where("OrderSerial", "<>", "");
        }

        $cp_area = Request::get('cp_area');
        if ( $cp_area) {
            $rw->where('cp_area','=',$cp_area);
        }
    }

    public static function detailPlayer($accountId){

        $tableAccount = 'platsdk_order';
        $data = \DB::connection('mysql_bgac')->table($tableAccount)->where('id',$accountId)->first();

        $data = (array)$data;
        //$data['playerList'] = json_decode($data['playerList'], true)?:[];
        $data['playerList'] = DB::connection('mysql_bgsi')->table('player_info')->where('account', $accountId)->get(['playerId', 'area as areaId', 'name as playerName','level', 'diamond'])->map(function($obj){return (array)$obj;})->toArray();

        $data['area_info'] = \App\Models\Area::areaAll();
        $data['area_info'] = array_column($data['area_info'], 'name', 'areaId');

        $style = $data['is_seal_number'] == 1 ? 'success':'default';
        $alias = $data['is_seal_number'] == 1 ? 'On':'Off';
        $data['seal_alias'] = '<span class="label label-'.$style.'">'.$alias.'</span>';

        $area = \Request::get("area") ?: $data['lastAreaId'];
        $rtn_info = [];
        $area_physics = \App\Models\Area::getPhysicsArea($area);
        
        \App\Models\SendSocketToGame::SendAction(function($socket)use(&$rtn_info, $data, $area_physics){
            $msg['msgid'] = \App\Service\MessageDef::http_plat_game_request;
            $msg["area"] = intval($area_physics);
            $msg['game_msgid'] = \App\Service\MessageDef::g2p_player_info_req;
            $msg["playerId"] = 1;
            $msg["game_msg"] = [
                'account' => $data['account'],
            ];
            $ret = $socket->send($msg);
            \Log::debug('send single player mail', []);

            $rtn_info = $socket->ReadHttpReponse();
            //\Log::debug('read mail reponse data', [$msg,$rtn_info]);
        });

        $data['player_info'] = $rtn_info['data'];
        \Log::Debug(__METHOD__, [$rtn_info, $data]);
        return $data;
    }

}